package com.baibei.module.stock.basic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.baibei.ebec.sdk.IQuotationApi;
import com.baibei.model.KLineEntry;
import com.baibei.module.LazyLoadFragment;
import com.baibei.module.stock.R;
import com.baibei.module.stock.adapter.KChartAdapter;
import com.baibei.module.stock.adapter.KLineNewEntity;
import com.baibei.module.stock.kline.chart.BaseKChartView;
import com.baibei.module.stock.kline.chart.KChartView;
import com.baibei.module.stock.kline.chart.formatter.DateFormatter;
import com.baibei.module.stock.kline.utils.DataHelper;
import com.baibei.quotation.event.QuotationPendingEvent;
import com.blankj.utilcode.utils.LogUtils;
import com.blankj.utilcode.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * K线基础
 */
public class BasicNewKLineFragment extends LazyLoadFragment implements StockKLineContract.View {

    protected String mType;
    private int mPosition = -1;
    private KChartView mChartView;
    private TextView mTvStopBusiness;
    private StockKLineContract.Presenter mPresenter;

    private String mProductInfo;
    private KChartAdapter mAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mType = getArguments().getString("type", IQuotationApi.TYPE_KLINE_5);
        mPosition = getArguments().getInt("position");
        mProductInfo = getArguments().getString("productInfo");
        mPresenter = inject(StockKLineContract.Presenter.class);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mChartView = view.findViewById(R.id.kchart_view);
        mTvStopBusiness = view.findViewById(R.id.tv_stop_business);

        mAdapter = new KChartAdapter();
        mChartView.setAdapter(mAdapter);
        mChartView.setDateTimeFormatter(new DateFormatter());
        mChartView.setGridRows(4);
        mChartView.setGridColumns(3);
        mChartView.setScaleX(1.3f);
        mChartView.setOnSelectedChangedListener(new BaseKChartView.OnSelectedChangedListener() {
            @Override
            public void onSelectedChanged(BaseKChartView view, Object point, int index) {
                KLineNewEntity data = (KLineNewEntity) point;
                Log.i("onSelectedChanged", "index:" + index + " closePrice:" + data.getClosePrice());
            }
        });
    }


    @Override
    public int getLayoutId(Bundle savedInstanceState) {
        return R.layout.fm_stock_new_kline_simple;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isInitData) {
            if (isVisibleToUser) {
                mPresenter.registerMarketStatus();
            } else {
                mPresenter.unRegisterMarketStatus();
                mPresenter.stopInterval();
            }
        } else {
            super.setUserVisibleHint(isVisibleToUser);
        }
    }


    @Override
    public void lazyLoad() {
        loadData();
    }

    /**
     * 加载数据
     */
    protected void loadData() {
        mPresenter.start();
        LogUtils.e("开始加载数据", ";byloadData");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        mPresenter.destroy();
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onLoadKLineData(List<KLineEntry> data) {
        mChartView.showLoading();
        final List<KLineNewEntity> lineEntries = new ArrayList<KLineNewEntity>();
        Log.i("KLineNewEntity", "加载开始");
        for (KLineEntry entry : data) {
            KLineNewEntity kLineNewEntity = new KLineNewEntity();
            kLineNewEntity.Open = entry.getOpen();
            kLineNewEntity.High = entry.getHigh();
            kLineNewEntity.Low = entry.getLow();
            kLineNewEntity.Close = entry.getClose();
            kLineNewEntity.Date = entry.getDay();

            lineEntries.add(kLineNewEntity);
        }
        DataHelper.calculate(lineEntries);
        Log.i("KLineNewEntity", "加载完成");
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                getActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
        mAdapter.addData(lineEntries);
        mChartView.startAnimation();
        mChartView.refreshEnd();
//                    }
//                });
//            }
//        }).start();
    }

    @Override
    public void onLoadFailed(String message) {
        ToastUtils.showShortToast(message);
    }

    @Override
    public String getType() {
        return mType;
    }

    @Override
    public String getProductId() {
        return mProductInfo;
    }

    @Override
    public void onTradeOutTime() {
        mTvStopBusiness.setVisibility(View.VISIBLE);
    }

    @Override
    public void onTradeOpenTime() {
        mTvStopBusiness.setVisibility(View.GONE);
    }


    @Subscribe
    public void onEvent(QuotationPendingEvent event) {
    }

}